<?php

require_once __DIR__ . '/vendor/autoload.php';

$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

echo "🔍 系统功能验证\n";
echo "================\n\n";

$errors = [];
$successes = [];

// 1. 检查数据库连接
echo "1️⃣ 检查数据库连接...\n";
try {
    \DB::connection()->getPdo();
    $successes[] = "✅ 数据库连接正常";
    echo "✅ 数据库连接正常\n";
} catch (Exception $e) {
    $errors[] = "❌ 数据库连接失败: " . $e->getMessage();
    echo "❌ 数据库连接失败: " . $e->getMessage() . "\n";
}

// 2. 检查核心表
echo "\n2️⃣ 检查核心数据表...\n";
$requiredTables = [
    'users', 'roles', 'user_roles', 'menus',
    'employees', 'departments', 'positions', 'personal_access_tokens'
];

foreach ($requiredTables as $table) {
    try {
        $count = \DB::table($table)->count();
        $successes[] = "✅ {$table}表存在，记录数: {$count}";
        echo "✅ {$table}表存在，记录数: {$count}\n";
    } catch (Exception $e) {
        $errors[] = "❌ {$table}表不存在或异常: " . $e->getMessage();
        echo "❌ {$table}表不存在或异常: " . $e->getMessage() . "\n";
    }
}

// 3. 检查用户和角色数据
echo "\n3️⃣ 检查用户和角色数据...\n";
try {
    $users = \DB::table('users')->count();
    $roles = \DB::table('roles')->count();
    $userRoles = \DB::table('user_roles')->count();

    if ($users > 0) {
        $successes[] = "✅ 存在 {$users} 个用户";
        echo "✅ 存在 {$users} 个用户\n";
    } else {
        $errors[] = "❌ 没有用户数据";
        echo "❌ 没有用户数据\n";
    }

    if ($roles > 0) {
        $successes[] = "✅ 存在 {$roles} 个角色";
        echo "✅ 存在 {$roles} 个角色\n";
    } else {
        $errors[] = "❌ 没有角色数据";
        echo "❌ 没有角色数据\n";
    }

    if ($userRoles > 0) {
        $successes[] = "✅ 存在 {$userRoles} 个用户角色关联";
        echo "✅ 存在 {$userRoles} 个用户角色关联\n";
    } else {
        $errors[] = "❌ 没有用户角色关联数据";
        echo "❌ 没有用户角色关联数据\n";
    }
} catch (Exception $e) {
    $errors[] = "❌ 检查用户角色数据失败: " . $e->getMessage();
    echo "❌ 检查用户角色数据失败: " . $e->getMessage() . "\n";
}

// 4. 检查菜单数据
echo "\n4️⃣ 检查菜单数据...\n";
try {
    $menus = \DB::table('menus')->count();

    if ($menus > 0) {
        $successes[] = "✅ 存在 {$menus} 个菜单项";
        echo "✅ 存在 {$menus} 个菜单项\n";

        // 检查菜单结构
        $rootMenus = \DB::table('menus')->whereNull('parent_id')->count();
        $childMenus = \DB::table('menus')->whereNotNull('parent_id')->count();

        echo "   - 根菜单: {$rootMenus} 个\n";
        echo "   - 子菜单: {$childMenus} 个\n";
    } else {
        $errors[] = "❌ 没有菜单数据";
        echo "❌ 没有菜单数据\n";
    }
} catch (Exception $e) {
    $errors[] = "❌ 检查菜单数据失败: " . $e->getMessage();
    echo "❌ 检查菜单数据失败: " . $e->getMessage() . "\n";
}

// 5. 检查控制器文件
echo "\n5️⃣ 检查控制器文件...\n";
$requiredControllers = [
    'BaseController.php',
    'MenuController.php',
    'RoleController.php',
    'PermissionController.php',
    'UserController.php',
    'EmployeeController.php',
    'DepartmentController.php',
    'PositionController.php'
];

foreach ($requiredControllers as $controller) {
    $filePath = __DIR__ . "/app/Http/Controllers/Api/{$controller}";
    if (file_exists($filePath)) {
        $successes[] = "✅ {$controller} 存在";
        echo "✅ {$controller} 存在\n";
    } else {
        $errors[] = "❌ {$controller} 不存在";
        echo "❌ {$controller} 不存在\n";
    }
}

// 6. 检查路由配置
echo "\n6️⃣ 检查路由配置...\n";
$routeFile = __DIR__ . '/routes/api.php';
if (file_exists($routeFile)) {
    $routeContent = file_get_contents($routeFile);

    $routePatterns = [
        'Route::post(\'/login\'' => '登录路由',
        'Route::get(\'/me\'' => '用户信息路由',
        'Route::get(\'/\', [MenuController::class, \'index\'])' => '菜单路由',
        'Route::get(\'/\', [RoleController::class, \'index\'])' => '角色路由',
        'Route::get(\'/\', [PermissionController::class, \'index\'])' => '权限路由',
        'Route::get(\'/\', [UserController::class, \'index\'])' => '用户管理路由',
        'Route::get(\'/\', [EmployeeController::class, \'index\'])' => '员工管理路由',
        'Route::get(\'/\', [DepartmentController::class, \'index\'])' => '部门管理路由',
        'Route::get(\'/\', [PositionController::class, \'index\'])' => '岗位管理路由'
    ];

    foreach ($routePatterns as $pattern => $description) {
        if (strpos($routeContent, $pattern) !== false) {
            $successes[] = "✅ {$description} 已配置";
            echo "✅ {$description} 已配置\n";
        } else {
            $errors[] = "❌ {$description} 未配置";
            echo "❌ {$description} 未配置\n";
        }
    }
} else {
    $errors[] = "❌ 路由文件不存在";
    echo "❌ 路由文件不存在\n";
}

// 7. 检查前端文件
echo "\n7️⃣ 检查前端文件...\n";
$frontendFiles = [
    'src/api/auth.ts',
    'src/stores/user.ts',
    'src/views/Login.vue',
    'src/views/Dashboard.vue',
    'src/views/system/Users.vue',
    'src/views/system/Roles.vue',
    'src/views/hr/Employees.vue',
    'src/views/hr/Departments.vue',
    'src/views/hr/Positions.vue'
];

foreach ($frontendFiles as $file) {
    $filePath = __DIR__ . '/../frontend/' . $file;
    if (file_exists($filePath)) {
        $successes[] = "✅ 前端文件 {$file} 存在";
        echo "✅ 前端文件 {$file} 存在\n";
    } else {
        $errors[] = "❌ 前端文件 {$file} 不存在";
        echo "❌ 前端文件 {$file} 不存在\n";
    }
}

// 8. 分析缺失的功能
echo "\n8️⃣ 分析系统功能完整性...\n";

$currentFeatures = [
    '用户管理', '角色权限管理', '菜单管理', '员工管理',
    '部门管理', '岗位管理', '基础认证'
];

$missingFeatures = [
    '考勤管理', '薪资管理', '绩效管理', '招聘管理',
    '培训管理', '合同管理', '社保管理', '公告通知',
    '报表统计', '系统设置', '审批流程', '工作流'
];

echo "📋 当前实现的功能:\n";
foreach ($currentFeatures as $feature) {
    echo "   ✅ {$feature}\n";
}

echo "\n📋 缺失的重要功能:\n";
foreach ($missingFeatures as $feature) {
    echo "   ❌ {$feature}\n";
}

echo "\n📊 功能覆盖率: " . round(count($currentFeatures) / (count($currentFeatures) + count($missingFeatures)) * 100, 1) . "%\n";

// 9. 总结报告
echo "\n================\n";
echo "📊 验证结果总结\n";
echo "================\n\n";

echo "✅ 成功的项目: " . count($successes) . "\n";
foreach ($successes as $success) {
    echo "   {$success}\n";
}

echo "\n❌ 失败的项目: " . count($errors) . "\n";
foreach ($errors as $error) {
    echo "   {$error}\n";
}

echo "\n" . (count($errors) === 0 ? "🎉 基础功能验证通过！" : "⚠️ 发现 " . count($errors) . " 个问题需要修复") . "\n";

echo "\n💡 建议: 系统目前只有基础功能，建议扩展以下模块:\n";
echo "   • 考勤管理 (打卡、请假、统计)\n";
echo "   • 薪资管理 (计算、发放、社保)\n";
echo "   • 绩效管理 (考核、评价、奖金)\n";
echo "   • 招聘管理 (职位发布、候选人、面试)\n";
echo "   • 培训管理 (计划、记录、评估)\n";
echo "   • 合同管理 (劳动合同、续签、到期)\n";
echo "   • 公告通知 (系统公告、消息推送)\n";
echo "   • 报表统计 (数据分析、可视化)\n";

?>
